<?php
define('IN_WONDERSHARE', true);
header('Content-type:text/html;charset=utf-8');
require('../conf/conf.php');
require('../includes/init.php');
require('./classes/module/module_template_api.php');
require('./configs/site_config.php');

class Api {
	
	private $mod, $smarty;
	private $api_type = array(
		'product'=>'产品模板接口',
		'buy'	 =>'购买模板接口',
		'article'=>'文章模板接口',
		'support'=>'Support接口',
		'company'=>'公司接口',
		'public' =>'公共接口'
	);
	
	function __construct(){
		$this->mod = new TemplateApiModule();
		$this->smarty = $GLOBALS['smarty'];
	}
	
	//note 查看
	function see(){
		global $site_name;
		$sid = $_GET['sid'] ? $_GET['sid'] : 0;
		$type = $_GET['type'];		
		$data = $this->mod->getList( $sid, $type );
		
		session_start();
		
		$this->smarty->assign('isadmin', $_SESSION['AID'] == 1 ? 1 : 0);
		$this->smarty->assign('site_name', $site_name);
		$this->smarty->assign('sid', $sid);
		$this->smarty->assign('type', $type);
		$this->smarty->assign('data', $data);
		$this->smarty->display('template_api.htm'); 
	}
	
	//ntoe 新增
	function add(){
		global $site_name;
		
		if( !empty( $_POST ) ){
			$name = $_POST['name'];
			$fun = $_POST['fun'];
			$type = $_POST['type'];
			$desc = $_POST['desc'];
			
			$tid = $this->mod->add( $name, $fun, $type, $desc );
			
			$site = explode(',', $_POST['site']);
			$detail = explode('*|*|*', $_POST['detail']);	
			foreach( $site as $k=>$v ){
				$this->mod->addDetail( $tid, $v, $detail[$k] );
			}
			
			echo 1;
			exit;
		}
		
		$type = $_GET['type'];
		
		$this->smarty->assign('tid', 0);
		$this->smarty->assign('site_name', $site_name);
		$this->smarty->assign('type', $type);
		$this->smarty->assign('api_type', $this->api_type);
		$this->smarty->display('template_api_add.htm');
	}
	
	//note 编辑
	function edit(){
		global $site_name;
		
		if( !empty( $_POST ) ){
			$tid = $_POST['tid'];
			$name = $_POST['name'];
			$fun = $_POST['fun'];
			$type = $_POST['type'];
			$desc = $_POST['desc'];
			
			$ret = $this->mod->change( $tid, $name, $fun, $type, $desc );	
			
			$site = explode(',', $_POST['site']);
			$detail = explode('*|*|*', $_POST['detail']);

			//先删除以前的内容，再添加
			$this->mod->delDetail( $tid );
			foreach( $site as $k=>$v ){
				$this->mod->addDetail( $tid, $v, $detail[$k] );
			}
			
			echo $ret ? 1 : 0;
			exit;
		}
		
		$tid = $_GET['tid'];		
		$data = $this->mod->getApi( $tid );
		$detail_data = $this->mod->getDetail( $tid );
		
		$this->smarty->assign('tid', $tid);
		$this->smarty->assign('type', $data['type']);
		$this->smarty->assign('api_type', $this->api_type);
		$this->smarty->assign('site_name', $site_name);
		$this->smarty->assign('data', $data);
		$this->smarty->assign('detail_data', $detail_data);
		$this->smarty->display('template_api_add.htm'); 
	}
	
	//note 显示API内容
	function show(){
		$tid = $_GET['tid'];
		$data = $this->mod->getApi( $tid );	
		
		$this->smarty->assign('tid', $tid);
		$this->smarty->assign('api_type', $this->api_type);
		$this->smarty->assign('data', $data);
		$this->smarty->display('template_api_show.htm');
	}
	
	//note 删除
	function del(){
		$tid = $_POST['tid'];
		$this->mod->del( $tid );
		echo 1;
	}
	
	//note 取得接口详细
	function get_detail(){
		global $site_name;
		
		$tid = $_GET['tid'];
		$data = $this->mod->getDetail( $tid );
		
		$site_name[0] = '所有站点';
		
		$this->smarty->assign('site_name', $site_name);
		$this->smarty->assign('data', $data);
		$this->smarty->display('template_api_detail.htm');
	}
	
	//note 模板新增说明
	function tpl_add_explain(){
		$this->smarty->display('template_add_explain.htm');
	}

}

$act = $_GET['action'];
$api = new Api();

if( method_exists( $api, $act ) ){
	$api->$act();
}else{
	die('no this function!');
}